<template>
    <el-container>
        <el-header>
            <h2><img src="../assets/logo.png" alt="">电商后台管理系统</h2>
            <el-popconfirm title="确认退出吗？" @confirm="exit">
                <el-button type="info" slot="reference">退出</el-button>
            </el-popconfirm>
        </el-header>
        <el-container>
            <el-aside :width="toggleWid ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleWid = !toggleWid">|||</div>
                <el-menu :default-active="$route.path" class="el-menu-vertical-demo" :collapse="toggleWid"
                    background-color="#545c64" text-color="#fff" active-text-color="#409eff" :collapse-transition="false"
                    router :unique-opened="true">

                    <el-submenu :index="item.id + ''" v-for="item in menus" :key="item.id">
                        <template slot="title">
                            <i :class="menu_icons[item.id]"></i>
                            <span slot="title">{{ item.authName }}</span>
                        </template>
                        <el-menu-item :index="'/' + item2.path" v-for="item2 in item.children" :key="item2.id">
                            <i class="el-icon-menu"></i>
                            {{ item2.authName }}
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-container>
                <el-main>
                    <router-view />
                </el-main>
            </el-container>
        </el-container>
    </el-container>
</template>
<script>
export default {
    data() {
        return {
            toggleWid: false,
            menus: [],
            menu_icons: {
                "125": "el-icon-user-solid",
                "103": "el-icon-s-cooperation",
                "101": "el-icon-s-goods",
                "102": "el-icon-s-order",
                "145": "el-icon-s-data"
            }
        }
    },
    methods: {
        exit() {
            this.$router.replace('/login');
            sessionStorage.removeItem('token');
        },
        async getMenu() {
            const { data: { data, meta } } = await this.$http.get('menus');
            if (meta.status !== 200) return this.$message.error('获取菜单失败')
            this.menus = data;
        }
    },
    created() {
        this.getMenu();
    }
}
</script>
<style scoped lang="less">
.el-header {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
}

.el-header {
    display: flex;
    justify-content: space-between;
    background-color: #373d41;

    h2 {
        margin: 0 0 0 50px;
        padding: 0;
        color: white;
        font-weight: normal;
        font-size: 1.3rem;

        img {
            width: 50px;
            height: 50px;
            position: absolute;
            left: 5px;
            top: 3px;
            border: 2px solid #585858;
            border-radius: 50%;
            background-color: #585858;
        }
    }
}

.el-aside {
    background-color: #373d41;
    color: #333;
    text-align: center;
    height: 100%;
}

.el-main {
    background-color: #eaedf1;
}

.el-container {
    width: 100%;
    height: 100%;
}

.el-container .el-aside {
    line-height: 320px;
}

.toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    color: #fff;
    line-height: 24px;
    text-align: center;
    letter-spacing: .2em;
    cursor: pointer;
}

/deep/.el-menu {
    border-right: 1px solid #545c64;
    text-align: left;
}
</style>